//
//  serviceGetDeviceInfo.m
//  upnpxdemo
//
//  Created by 赵帅 on 13-8-31.
//  Copyright (c) 2013年 wiimu.inc. All rights reserved.
//

#import "serviceGetDeviceInfo.h"
#import "privateJSONParser.h"

@implementation serviceGetDeviceInfo
#pragma mark Initialization & Deallocation

-(id)initWithDelegate:(id <serviceGetDeviceInfoDelegate>) aDelegate
{
	self = [super init];
	if (self)
	{
		self.m_serviceHandler = aDelegate;
	}
    
	return self;
}

#pragma mark -
#pragma mark Configure the request
-(NSURL*)targetURL
{
    NSString * ipStr = [m_dicSendData objectForKey:@"baseurl"];
    NSString *urlStr = [NSString stringWithFormat:@"%@/info.html",ipStr];
	NSURL *theUrl = [[NSURL alloc] initWithString:urlStr];
	
	return theUrl;
}

-(void)configureRequest
{
	[super configureRequest];
	[m_request setRequestMethod:@"GET"];
}

#pragma mark -
#pragma mark Parse the response
-(void)parseResponseData:(NSData *)data
{
	[super parseResponseData:data];

    NSLog(@"data length:%d",data.length);
    NSMutableString *retStr = [[NSMutableString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    //NSLog(@"parseResponseData: %@", retStr);
    
	//NSMutableString * retStr = [NSMutableString stringWithString:m_request.responseString];
    if(retStr == nil)
    {
        [m_dicReceiveData setObject:SERVICE_FAIL forKey:@"responsestatus"];
        return;
    }

    NSLog(@"retstr:%@",retStr);
    
    [retStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [retStr length])];
    if([retStr objectFromJSONString] != nil)
    {
        [m_dicReceiveData addEntriesFromDictionary:[retStr objectFromJSONString]];
    }
    else
    {
        NSString* sub = nil;

        privateJSONDParser *parser = [[privateJSONDParser alloc] initWithObjectAndDic:retStr withDic:m_dicReceiveData];
        
        sub = [parser parseObject:@"ssid"];
        if(sub)
        {
            sub = [parser parseObject:@"expired"];
            if(sub)
            {
                sub = [parser parseObject:@"netstat"];
                if(sub)
                {
                    sub = [parser parseObject:@"internet"];
                    if(sub)
                    {
                        sub = [parser parseObject:@"essid"];
                        if(sub)
                        {
                            sub = [parser parseObject:@"apcli0"];
                            if(sub)
                            {
                                sub = [parser parseObject:@"uuid"];
                                if(sub)
                                {
                                    sub = [parser parseObject:@"firmware"];
                                    if(sub)
                                    {
                                        sub = [parser parseObject:@"language"];
                                        if(sub)
                                        {
                                            sub = [parser parseObject:@"eth2"];
                                            if(sub)
                                            {
                                                sub = [parser parseObject:@"hardware"];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if(sub == nil)
        {
            //haiyong 能不能定义另外一个变量，例如PARSE_FAIL，把这种错误与连接错误区分开来，只有连接错误才返回设备列表，
            [m_dicReceiveData setObject:SERVICE_FAIL forKey:@"responsestatus"];
            return;
        }
    }
    
    //uuid前面有一个空格 删之
    if([[m_dicReceiveData objectForKey:@"uuid"] hasPrefix:@" "])
    {
        [m_dicReceiveData setObject:[[m_dicReceiveData objectForKey:@"uuid"] substringFromIndex:1] forKey:@"uuid"];
    }
    
    [m_dicReceiveData setObject:[m_dicSendData objectForKey:@"baseurl"] forKey:@"sendBaseURL"];
    NSLog(@"serviceGetDeviceInfo:%@",m_dicReceiveData);
}

#pragma mark -
#pragma mark Pass Data To UI
-(void)passDataOut
{
	[super passDataOut];
	
	if (nil != self.m_serviceHandler
		&& [self.m_serviceHandler respondsToSelector:@selector(serviceGetDeviceInfoCallBack:)])
	{
        [self.m_serviceHandler performSelectorOnMainThread:@selector(serviceGetDeviceInfoCallBack:) withObject:m_dicReceiveData waitUntilDone:YES];
	}
}

@end
